home *** CD-ROM | disk | FTP | other *** search
- /*
- * tesselate.c - open a window, clear the background, and render
- * a simple nonconvex polygon with and without tesselation.
- * The untesselated polygon is drawn (incorrectly) on the left.
- * The corresponding tesselated object is drawn on the right.
- *
- * The tesselated object is stored in a display list.
- *
- * Invoking this program with any argument causes the callbacks to
- * produce debugging output.
- */
- #include <stdio.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <aux.h>
-
- /* Function Prototypes */
-
- GLvoid initialize( int, char ** );
- GLvoid drawScene( GLvoid );
- GLvoid resize( GLsizei, GLsizei );
-
- void tesserror( GLenum errno );
- void my_glBegin( GLenum mode);
- void my_glEnd( void);
- void my_glVertex2fv( const GLfloat *v);
-
- #define Dprintf if (debug) printf
- int debug;
-
-
- void
- main( int argc, char *argv[] )
- {
- initialize( argc, argv );
-
- auxMainLoop( drawScene );
- }
-
- #define NPOLYVERTS (sizeof(polyVerts)/sizeof(polyVerts[0]))
- #define COLOR_OFFSET 2 /* color offset in array of info about vertex */
- #define VSIZE 5
- /* X Y R G B */
- static GLfloat polyVerts[][VSIZE]={
- { -2, 2, 0, 0, 0 },
- { -2, -2, 0, 0, 1 },
- { 2, -2, 0, 1, 0 },
- { 2, 2, 0, 1, 1 },
- { 0, 0, 1, 0, 0 }
- };
-
- static GLUtriangulatorObj *tObj = NULL;
- static GLuint tObjList;
-
- GLvoid
- initialize( int argc, char *argv[] )
- {
- GLsizei width, height, winSize;
-
- int i;
- GLdouble vertex[3];
-
- if (argc > 1)
- debug = 1;
- else
- debug = 0;
-
- auxGetScreenSize( &width, &height );
- winSize = ( width < height ) ? width : height;
- auxInitPosition( winSize / 4, winSize / 4, winSize / 2,
- winSize / 2 );
- auxInitDisplayMode( AUX_RGBA );
- auxInitWindow( argv[0] );
-
- auxReshapeFunc( resize );
- auxKeyFunc( AUX_ESCAPE, auxQuit );
-
- glClearColor( 0.0, 0.0, 1.0, 1.0 );
-
- glPointSize( 3.5 );
- glLineWidth( 2.5 );
-
- /* Create the tesselation object */
- tObj = gluNewTess();
- gluTessCallback(tObj, GLU_BEGIN, my_glBegin);
- gluTessCallback(tObj, GLU_VERTEX, my_glVertex2fv);
- gluTessCallback(tObj, GLU_END, my_glEnd);
- gluTessCallback(tObj, GLU_ERROR, tesserror);
-
- tObjList = glGenLists(1);
- glNewList( tObjList, GL_COMPILE );
- gluBeginPolygon(tObj);
- for ( i = 0; i < NPOLYVERTS; i++ ) {
- vertex[0] = polyVerts[i][0];
- vertex[1] = polyVerts[i][1];
- vertex[2] = 0;
- /* vertex[] is the vertex of the polygon
- * being tesselated. polyVerts[i] is
- * the vertex plus associated data
- * (color, in this case) that get passed
- * (by the GLU_VERTEX callback) to the
- * my_glVertex2fv func. */
- gluTessVertex(tObj, vertex, &polyVerts[i]);
- }
- gluEndPolygon(tObj);
- glEndList();
-
- glMatrixMode( GL_PROJECTION );
- /* Set up the world coordinates we want */
- glOrtho( -10.0, 10.0, -10.0, 10.0, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- resize( GLsizei width, GLsizei height )
- {
- GLdouble aspect, left, right, bottom, top;
-
- glViewport( 0, 0, width, height );
-
- /* compute aspect ratio */
- aspect = (GLdouble) width / (GLdouble) height;
-
- /* make sure the window goes from [-10.0, 10.0] in the
- smallest dimension */
- if ( aspect < 1.0 ) {
- left = -10.0;
- right = 10.0;
- bottom = -10.0 * ( 1.0 / aspect );
- top = 10.0 * ( 1.0 / aspect );
- } else {
- left = -10.0 * aspect;
- right = 10.0 * aspect;
- bottom = -10.0;
- top = 10.0;
- }
-
- glMatrixMode( GL_PROJECTION );
- /* Reset world coordinates first ... */
- glLoadIdentity();
-
- /* Then set them to what we want based on the new aspect ratio*/
- glOrtho( left, right, bottom, top, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i;
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- /* Draw untesselated object (incorrectly) */
- glPushMatrix();
- glTranslatef( -5.0, 0.0, 0.0 );
- glBegin( GL_POLYGON );
- for (i=0; i < NPOLYVERTS; i++) {
- glColor3fv( polyVerts[i]+COLOR_OFFSET );
- glVertex2fv( polyVerts[i] );
- }
- glEnd();
- glPopMatrix();
-
- /* Draw tesselated objects (correctly) */
- glPushMatrix();
- glTranslatef( 5.0, 0.0, 0.0 );
- glCallList( tObjList );
- glPopMatrix();
-
- glFlush();
- auxCheckError("drawScene");
- }
-
- /* The next four routines are for tesselation callbacks. */
-
- /* Called if there is an error during tesselation. */
- void
- tesserror ( GLenum errno )
- {
- printf( "tesselation error: %s\n", gluErrorString( errno ) );
- fflush( stdout );
- auxQuit();
- }
-
- /* Called when a new primitive is started. */
- void
- my_glBegin(GLenum mode)
- {
- Dprintf ("my_glBegin: mode is %d\n", mode);
- glBegin (mode);
- }
-
- /* Called when a primitive is finished. */
- void
- my_glEnd(void)
- {
- Dprintf ("my_glEnd:\n");
- glEnd();
- }
-
- /* Called when a vertex of a primitive is generated by the
- * tesselator. The parameter v is a pointer to the data
- * that was passed to the GLU_VERTEX callback. In this case,
- * the first two elements are coordinates of the vertex
- * and the next three specify the vertex color. */
- void
- my_glVertex2fv(const GLfloat *v)
- {
- Dprintf ("my_glVertex2fv: v[0] = %g, v[1] = %g ", v[0], v[1]);
- Dprintf ("R = %g, G = %g, B = %g\n", v[2], v[3], v[4]);
-
- glColor3fv(v+COLOR_OFFSET);
- glVertex2fv (v);
- }
-